<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="反馈对象" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
              placeholder="请选择资源类型"
              v-for="item in feedUser"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="反馈" prop="intro">
        <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="dataForm.intro">
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj, feedUser} from '@/api/teacher/feedback'

export default {
  data() {
    return {
      feedUser: [],
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        intro: '',
        type: ''
      },
      dataRule: {
        intro: [
          {required: true, message: '反馈不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '反馈对象不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.queryFeedUser()
  },
  methods: {
    queryFeedUser(){
      feedUser(this.dataForm).then(data => {
       this.feedUser = data.data
      }).catch(() => {
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
